﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace FYPDemo
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["page"] = "register";
                bindSecurityQuestionDDL();
                bindStateDDL();
            }
        }

        protected void bindSecurityQuestionDDL()
        {
            DataTable dt = new DataTable();
            
            dt = SecurityManagement.listSecurityQuestion();

            ddlSecurityQuestion.DataSource = dt;
            ddlSecurityQuestion.DataTextField = "SecurityQuestion";
            ddlSecurityQuestion.DataValueField = "ID";
            ddlSecurityQuestion.DataBind();
            
           
        }

        protected void bindStateDDL()
        {
            DataTable dt = new DataTable();

            dt = Common.bindState();

            ddlState.DataSource = dt;
            ddlState.DataTextField = "State";
            ddlState.DataValueField = "ID";
            ddlState.DataBind();
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            int result = 0;

            string name = txtName.Text.Trim();
            string password = txtPassword.Text.Trim();
            string email = txtEmail.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string address = txtAddress.Text.Trim();
            string postcode = txtPostCode.Text.Trim();
            string state = ddlState.SelectedItem.ToString();
            int role = Convert.ToInt32(rbCategory.SelectedValue);
            int SecurityQ = Convert.ToInt32(ddlSecurityQuestion.SelectedValue);
            string answer = txtAnswer.Text.Trim();
            string DP = DisplayPicture.ImageUrl.ToString();

            if(name != "" && DP != "")
            {
                System.IO.File.Delete(Server.MapPath(DP));
                HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                uploadFile(theFile, "DisplayPicture", name);
                DisplayPicture.ImageUrl = "~/DisplayPicture/" + name + ".jpg";
                DP = DisplayPicture.ImageUrl.ToString();
            }
            
            string subject = "Successfully register as member of Universale";
            string body = "Dear Mr/Mrs " + name + "<br /><br />Congratulation for successfully register as member of Universale!";
                        

            int dtr = Member.checkUserName(name);

            if (dtr > 0)
            {
                MessageBox.Show("This name already been registered. Please choose another name.");
            }
            
            else
            {   
                result = Member.registernewMember(name, password, email, phone, address, postcode, state, role, DP, SecurityQ, answer);

                if (result > 0)
                {
                    SendMail.SendEmail(email, subject, body);
                    MessageBox.Show("Successfully register as a member of Universale.");
                }

                else
                {
                    MessageBox.Show("There is some error during the registration process. Please try again later or contact our support team.");
                }
             }                
        }       

        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string username = "TempUser";
            string uploadTo = "DisplayPicture";
            
            if (FileUpload1.HasFile)
            {
                if(Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + username)))
                {
                        System.IO.File.Delete(Server.MapPath("~/" +uploadTo+ "/" +username));
                }

                else
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, username);
                    DisplayPicture.ImageUrl = "~/" + uploadTo + "/" + username + ".jpg";
                }
            } 
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string path = DisplayPicture.ImageUrl.ToString();

            if (path != "")
            {
                DisplayPicture.ImageUrl = "";
                System.IO.File.Delete(Server.MapPath(path));
            }
        }       
    }
}